package pers.liuchengyin.constants;

import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName MappingProperty
 * @Description
 * @Author 柳成荫
 * @Date 2022/4/1
 */
public class MappingProperty extends HashMap<String, Object> {

    /******************** 映射类型 Start ********************/

    public static final String TEXT = "text";

    public static final String KEYWORD = "keyword";

    public static final String BYTE = "byte";

    public static final String SHORT = "short";

    public static final String INTEGER = "integer";

    public static final String LONG = "long";

    public static final String FLOAT = "float";

    public static final String HALF_FLOAT = "half_float";

    public static final String SCALED_FLOAT = "scaled_float";

    public static final String DOUBLE = "double";

    public static final String DATE = "date";

    public static final String INTEGER_RANGE = "integer_range";

    public static final String LONG_RANGE = "long_range";

    public static final String FLOAT_RANGE = "float_range";

    public static final String DOUBLE_RANGE = "double_range";

    public static final String DATE_RANGE = "date_range";

    public static final String BOOLEAN = "boolean";

    public static final String BINARY = "binary";

    public static final String OBJECT = "object";

    public static final String NESTED = "nested";

    public static final String IP = "ip";

    /******************** 映射类型 end ********************/


    /******************** 分词器类型 Start ********************/

    public static final String ANALYZER = "analyzer";

    public static final String SEARCH_ANALYZER = "search_analyzer";

    /******************** 分词器类型 end ********************/


    /******************** 分词器粒度 Start ********************/

    public static final String ANALYZER_IK_SMART = "ik_smart";

    public static final String ANALYZER_IK_MAX_WORD = "ik_max_word";

    /******************** 分词器粒度 end ********************/

    public MappingProperty() {
    }

    public MappingProperty(String type) {
        this.put("type", type);
        // 日期格式处理
        if (DATE.equals(type)) {
            this.put(EsConstants.DATE_FORMAT, EsConstants.DATE_FORMAT_PATTERN);
        } else if (TEXT.equals(type)) {
            // text类型字段使用IK分词器
            this.put(ANALYZER, ANALYZER_IK_MAX_WORD);
            this.put(SEARCH_ANALYZER, ANALYZER_IK_MAX_WORD);
        }
    }

    public MappingProperty(String type, boolean createKeyword) {
        this(type);
        if (createKeyword) {
            this.put("fields", createKeyword());
        }
    }

    public MappingProperty(String type, Map<String, MappingProperty> nestedProperties) {
        this(type);
        if (!CollectionUtils.isEmpty(nestedProperties)) {
            this.put(EsConstants.INDEX_MAPPING_PROPERTIES, nestedProperties);
        }
    }

    private Map<String, Object> createKeyword() {
        Map<String, Object> keyword = new HashMap<>();
        keyword.put("type", KEYWORD);
        keyword.put("ignore_above", 256);
        Map<String, Object> fields = new HashMap<>();
        fields.put("fields", keyword);
        return fields;
    }

}
